// pages/shop/index.js
const app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopList: [],
    codeUrl: "",
    canIUse: true,
    userInfo: {},
    newArr: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.showListFun();
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        canIUse: false,
        userInfo: userInfo
      })
      //const openid = wx.getStorageSync('openid');
      //this.setLoginByUserInfo(openid, userInfo);
    }
  },
  showListFun() {
    var that = this
    var shopList = wx.getStorageSync('shopList')
    var shopListKeys = Object.keys(shopList).reverse();
    var newArr = that.reArr(shopListKeys);
    shopListKeys = Object.keys(newArr).sort().reverse();
    that.setData({
      newArr: newArr,
      shopListKeys: shopListKeys,
      shopList: shopList
    })
  },
  reArr: function(arr) {
    var arrLen = arr.length
    var newArr = {};
    for (var i = 0; i < arrLen; i++) {
      var date = arr[i]
      arr[i] = arr[i].replace('年', '-');
      arr[i] = arr[i].replace('月', '-');
      arr[i] = arr[i].replace('日', '');
      newArr[new Date(arr[i]).getTime()] = date;
    }
    return newArr;
  },
  handleClick() {
    // console.log(111);
  },
  //扫描二维码
  openScanCode() {
    wx.scanCode({
      success: (res) => {
        var codeUrl = res.result;
        codeUrl = codeUrl.replace("https://www.leuar.com/qrcode/", "")
        wx.navigateTo({
          url: 'info?id=' + codeUrl
        })
      }
    })
  },
  //进入详情页面
  goToShopInfo(e) {
    wx.navigateTo({
      url: 'info?id=' + e.currentTarget.dataset.id || this.data.codeUrl
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.showListFun();
    // var time = util.formatDate(new Date())
    // this.setData({
    //   shijian:time
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  bindGetUserInfo: function(e) {
    //console.log(e.detail.userInfo);
    const openid = wx.getStorageSync('openid');
    const userInfo = e.detail.userInfo;
    this.setLoginByUserInfo(openid, userInfo);
  },
  setLoginByUserInfo: function(openid, userInfo) {
    const that = this;
    wx.request({
      url: app.apiURL + 'auth/getUserInfo',
      data: {
        openid: openid,
        userInfo: userInfo
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res);
        if (res.data) {
          if (res.data.errno == 0) {
            console.log(res.data.data);
            //app.globalData.userInfo = JSON.parse(res.data.data);
            //wx.setStorageSync('openid', res.data.openId);
            wx.setStorageSync('userInfo', res.data.data);
          }
        }
        that.openScanCode();
      }
    })
  },
})